home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / artcpy20.zip / SAMPLE2.C < prev   
Text File  |  1991-04-01  |  10KB  |  311 lines

  1. /* =======================================================================   */
  2. /*           SAMPLE2.C                                                       */
  3. /*    This is a sample of a C program which uses BGIPrint drivers.           */
  4. /*    It uses portions of Borland's BGI Demo to illustrate the compatibility */
  5. /*    of BGI Print with screen drivers.                                      */
  6. /*                                                                           */
  7. /*    Please note the use of calculated x-y coordinates based on values      */
  8. /*    returned by GETMAXX and GETMAXY.                                       */
  9. /*                                                                           */
  10. /*    To run this sample, ensure that the BGIPrint drivers are in the        */
  11. /*    current directory along with the CHR files supplied with Turbo C       */
  12. /*                                                                           */
  13. /*                                                                           */
  14. /*   Written by Glen McDonald                                                */
  15.  
  16. #include <graphics.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include <conio.h>
  23.  
  24.  
  25. #define adjasp( y ) ((int)(AspectRatio * (double)(y)))
  26. #define PI          3.14159         /* Define a value for PI    */
  27. #define CLIP_ON     1
  28. #define TRUE        1
  29. #define FALSE       0
  30.  
  31.  
  32. /* ---------- Global Variables ---------- */
  33.  
  34. int     driver, mode, testdriver, errcode,
  35.         maxX, maxY, maxcolor, fillcolor,
  36.         numpens = 8;                   /* plotter has this many pens */
  37.  
  38. char    prname[7];
  39. double  AspectRatio;
  40.  
  41. /* ------------ Function Prototypes ----------- */
  42. void intro(void);
  43. void initprinter(void);
  44. void pieplay(void);
  45. void bar3dplay(void);
  46. void gettextcoords(int AngleInDegrees, int *radius, int *x, int *y);
  47. int huge testdetect(void);
  48.  
  49.  
  50. int main(void)
  51.     {
  52.     intro();
  53.     initprinter();
  54.     pieplay();
  55.     printf("\a");
  56.     printf("Press any key for\n");
  57.     printf("3D bar demo\n");
  58.     getch();
  59.     closegraph();
  60.     initprinter();
  61.     bar3dplay();
  62.     printf("\a");
  63.     printf("Press any key to quit\n");
  64.     getch();
  65.     closegraph();
  66.     return 0;
  67.     }
  68.  
  69. void intro(void)
  70.     {
  71.     char *bgidrvrs[] = { "HPPCL", "MATRIX", "HPGL", "SCREEN" };
  72.     int ch, needchoice = TRUE;
  73.  
  74.     clrscr();
  75.     printf("                               BGI Print\n");
  76.     printf("                    Copyright (c) Bruce McAra, 1991\n");
  77.     printf("                           All Rights Reserved\n");
  78.     printf("                   Demonstration Program for Turbo C\n");
  79.     printf("            ***************************************************\n");
  80.     printf("                1. HP Laser Jet (PCL) on LPT1\n");
  81.     printf("                2. Epson Compatible Dot Matrix on LPT1\n");
  82.     printf("                3. HPGL Pen Plotter on COM1\n");
  83.     printf("                4. Screen\n\n");
  84.     printf("                     Select a printer Driver by Number:  ");
  85.  
  86.     do
  87.         {
  88.         ch = getch();
  89.         if((ch >= 0x31 && ch <= 0x34) || ch == 0x1B)
  90.             needchoice = FALSE;
  91.         } while (needchoice == TRUE);
  92.  
  93.     if (ch == 0x1B)
  94.         {
  95.         printf("Terminating program\n");
  96.         exit(0);
  97.         }
  98.     else
  99.         {
  100.         strcpy(prname, bgidrvrs[ch-0x31]);
  101.         printf("\nPrinting to %s\n", prname);
  102.         }
  103.     }
  104.  
  105.  
  106. void initprinter(void)
  107.     {
  108.     /* Install the driver */
  109.     if (!strcmp(prname, "HPGL"))
  110.         {
  111.         printf("Please insert paper into plotter and Press any key when ready");
  112.         getch();
  113.         }
  114.  
  115.     if (strcmp(prname, "SCREEN"))
  116.         {
  117.         testdriver = installuserdriver(prname, testdetect);
  118.         if (graphresult() != grOk)
  119.             {
  120.             printf("Error installing driver\n");
  121.             exit(1);
  122.             }
  123.         driver = DETECT;
  124.         printf("Initializing graphics buffer\n");
  125.         initgraph(&driver, &mode, "");
  126.         errcode = graphresult();
  127.         if (errcode != grOk)
  128.             {
  129.             printf("Error during Init: %s\n", grapherrormsg(errcode));
  130.             exit(1);
  131.             }
  132.         else
  133.             printf("INIT OK\n");
  134.         }
  135.     else
  136.         {
  137.         driver = DETECT;
  138.         initgraph(&driver, &mode, "");
  139.         }
  140.     maxX = getmaxx();
  141.     maxY = getmaxy();
  142.     if (!strcmp(prname, "HPGL"))
  143.         maxcolor = numpens;
  144.     else
  145.         maxcolor = getmaxcolor();
  146.     }
  147.  
  148. void pieplay(void)
  149.     {
  150.     struct viewporttype vp;
  151.     int centerX, centerY, x, y;
  152.     int radius, xasp, yasp;
  153.     double piesize;
  154.  
  155.     printf("Doing Pie Chart\n");
  156.     getaspectratio(&xasp, &yasp);
  157.     AspectRatio = (double)xasp / (double)yasp;
  158.     getviewsettings( &vp );               /* Get the current viewport */
  159.     centerX = (vp.right - vp.left) / 2;   /* Center the Pie horizontally  */
  160.     centerY = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically    */
  161.     radius  = (vp.bottom - vp.top) / 3;   /* It will cover 2/3rds screen  */
  162.     piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie  */
  163.  
  164.     while( (AspectRatio*radius) < piesize ) ++radius;
  165.  
  166.     settextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  167.     settextjustify( CENTER_TEXT, TOP_TEXT );
  168.     outtextxy( maxX/2, 6, "This is a Pie Chart" );
  169.     printf("Title Done\n");
  170.  
  171.     settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  172.     settextjustify( CENTER_TEXT, TOP_TEXT );
  173.  
  174.     fillcolor = 2;
  175.     printf("Fill color = %d\n", fillcolor);
  176.     setfillstyle( SOLID_FILL, fillcolor );
  177.     pieslice( centerX+10, centerY-adjasp(10), 0, 90, radius );
  178.     gettextcoords(45, &radius, &x, &y);
  179.     settextjustify( LEFT_TEXT, BOTTOM_TEXT );
  180.     outtextxy( centerX+10+x+textwidth((char far *)"H"), centerY-adjasp(10+y), "25 \%");
  181.     printf("Segment Done\n");
  182.  
  183.     fillcolor = 3;
  184.     printf("Fill color = %d\n", fillcolor);
  185.     setfillstyle( LTSLASH_FILL, fillcolor);
  186.     pieslice( centerX, centerY, 225, 360, radius );
  187.     gettextcoords(293, &radius, &x, &y);
  188.     settextjustify( LEFT_TEXT, TOP_TEXT );
  189.     outtextxy( centerX+x+textwidth("H"), centerY-adjasp(y), "37.5 \%");
  190.     printf("Segment Done\n");
  191.     printf("Fill color = %d\n", fillcolor = 4);
  192.     setfillstyle( BKSLASH_FILL, fillcolor );
  193.     pieslice(centerX-10, centerY, 135, 225, radius);
  194.     gettextcoords(180, &radius, &x, &y);
  195.     settextjustify( RIGHT_TEXT, CENTER_TEXT );
  196.     outtextxy(centerX-10+x-textwidth("H"), centerY-adjasp(y), "25 \%");
  197.     printf("Segment Done\n");
  198.  
  199.     printf("Fill color = %d\n", fillcolor = 5);
  200.     setfillstyle(WIDE_DOT_FILL, fillcolor);
  201.     pieslice(centerX, centerY, 90, 135, radius);
  202.     gettextcoords(112, &radius, &x, &y);
  203.     settextjustify(RIGHT_TEXT, BOTTOM_TEXT);
  204.     outtextxy(centerX+x-textwidth("H"), centerY-adjasp(y), "12.5 \%");
  205.     printf("Chart done\n");
  206.     }
  207.  
  208. void bar3dplay(void)
  209.     {
  210.     int numbars = 7;
  211.     int barheight[] = {1,3,2,4,3,2,1};
  212.     int Yticks = 5;                     /* The number of tick marks on the
  213.                                          * Y axix */
  214.     struct viewporttype vp;
  215.     unsigned h, depth, color, t;
  216.     int i, j, xstep, ystep;
  217.     char buffer[10];
  218.  
  219.     printf("3d Bar Chart\n");
  220.     h = 3 * textheight("M");
  221.     t = 2 * textheight("M");
  222.     getviewsettings(&vp);
  223.     settextjustify(CENTER_TEXT, TOP_TEXT);
  224.     settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
  225.     outtextxy(maxX / 2, t, "These are 3D bars");
  226.     printf("Title done\n");
  227.     settextstyle(SMALL_FONT, HORIZ_DIR, 4);
  228.     setviewport(vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, CLIP_ON);
  229.     getviewsettings(&vp);
  230.     line(h, h, h, (vp.bottom - vp.top) - h);
  231.     line(h, (vp.bottom - vp.top) - h, (vp.right - vp.left) - h,
  232.                                         (vp.bottom - vp.top) - h);
  233.     ystep = ((vp.bottom - vp.top) - (2*h)) / Yticks;
  234.     xstep = ((vp.right - vp.left) - (2*h)) / numbars;
  235.     j = (vp.bottom - vp.top) - h;
  236.     t = textwidth("M");
  237.     settextjustify(CENTER_TEXT, CENTER_TEXT);
  238.  
  239.     /* Draw the Y axis and tick marks */
  240.     for(i = 0; i < Yticks; ++i)
  241.         {
  242.         line(h / 2, j, h, j);
  243.         itoa(i, buffer, 10);
  244.         outtextxy(h/2 - t, j, buffer);
  245.         j -= ystep;
  246.         }
  247.     printf("Y axis done\n");
  248.     depth = (int)(0.25 * xstep);        /* Calculate depth of bar */
  249.  
  250.     /* Draw X axis, bars, and tick marks */
  251.     j = h;
  252.     settextjustify(CENTER_TEXT, TOP_TEXT);
  253.     color = 1;
  254.     for ( i = 1; i <= numbars + 1; i++)
  255.         {
  256.         setcolor(1);
  257.         line(j, (vp.bottom-vp.top)- h, j, (vp.bottom-vp.top-3)-(h/2));
  258.         itoa(i-1, buffer, 10);
  259.         outtextxy(j, (vp.bottom-vp.top)-(t*2), buffer);
  260.         printf("%s\n", buffer);
  261.         if (i != numbars+1)
  262.             {
  263.             color = color + 1;
  264.             setfillstyle(i, color);
  265.             setcolor(color);
  266.             bar3d(j,
  267.                   ((vp.bottom-vp.top-h)-(barheight[i-1] * ystep)),
  268.                   j+xstep-depth, ((vp.bottom-vp.top)-h-1), depth,1);
  269.             j += xstep;
  270.             printf("Bar Done\n");
  271.             }
  272.         }
  273.     printf("Chart Done\n");
  274.     }
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. void gettextcoords(int AngleInDegrees, int *radius, int *x, int *y)
  283.     {
  284.     double radians;
  285.  
  286.     radians = AngleInDegrees * PI / 180;
  287.     *x = cos(radians) * *radius + 0.5;
  288.     *y = sin(radians) * *radius + 0.5;
  289.     }
  290.  
  291. int huge testdetect(void)
  292.     {
  293.     /* Autodetect function.  Assume hardware is always present.
  294.      * Return value = recommended default mode */
  295.  
  296.     return 1;
  297.     }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310. 
  311.